package com.neil.dynamic.manage.controller;

import com.neil.dynamic.manage.config.RedisConfig;
import com.neil.dynamic.manage.entity.DynamicVersion;
import com.neil.dynamic.manage.service.inf.IDynamicVersionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 *  Router版本管理
 * @ProjectName:
 * @Package:        com.neil.dynamic.manage.controller
 * @ClassName:      DynamicVersionController
 * @Description:    Router版本管理
 * @Author:         Neil.Zhou
 * @CreateDate:     2019/10/14 17:41
 * @UpdateUser:     Neil.Zhou
 * @UpdateDate:     2019/10/14 17:41
 * @UpdateRemark:   The modified content
 * @Version:        1.0
 * <p>Copyright: Copyright (c) 2019/10/14</p>
 *
 */
@Slf4j
@Controller
@RequestMapping("/version")
public class DynamicVersionController {

    @Autowired
    private IDynamicVersionService dynamicVersionService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 发布版本
     * @param map
     * @return
     */
    @RequestMapping(value = "/add" , method = RequestMethod.GET)
    public String add(ModelMap map){
        DynamicVersion version = new DynamicVersion();
        int result = dynamicVersionService.add(version);
        map.addAttribute("result" , result);
        return "addVersion";
    }

    /**
     * 最后版本号
     * @return
     */
    @RequestMapping(value = "/lastVersion" , method = RequestMethod.GET)
    @ResponseBody
    public Long getLastVersion(){
        Long versionId = 0L;
        String result = redisTemplate.opsForValue().get(RedisConfig.versionKey);
        if(!StringUtils.isEmpty(result)){
            log.info("返回 redis 中的版本信息......");
            versionId = Long.valueOf(result);
        }else{
            log.info("返回 mysql 中的版本信息......");
            versionId = dynamicVersionService.getLastVersion();
            redisTemplate.opsForValue().set(RedisConfig.versionKey , String.valueOf(versionId));
        }
        return versionId;
    }


    //打开发布版本列表页面
    public String listAll(ModelMap map){
        map.addAttribute("list", dynamicVersionService.listAll());
        return "versionlist";
    }
}
